package org.ChrisRiffle.Acme.BoundingBox.controlers;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 * 
 * Can play sounds.  
 * 
 * Recommended to initialize it on app initialization 
 * instead of the first time a sound is played
 * 
 * @author RIF
 *
 */
public enum AudioController {
	
	//singleton
	INSTANCE;


	private static final String MARIO_SOUND_CLASSPATH = "res/marioCoinSound.wav";
	
	private InputStream marioSoundInputStream;
	

	
	/**
	 * Loads sounds from disk(potentially $$ expensive)
	 */
	public void init(){
			
			//load it from disk
			try {
				
				getMarioSoundFile();
				
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
	}
	
	/**
	 * Plays the Mario Sound File in a new thread(spawns one)
	 * 
	 */
	public void playMarioSoundInNewThread(){
		
		 new Thread(new Runnable() {
		      public void run() {
		      
		    	  playMarioSound();
		      }
		    }).start();
		  
	}
	
  
	/**
	 * 
	 * Plays the Mario Sound file in the current execution line,
	 * (doesn't spawn a new thread)
	 * 
	 * @throws LineUnavailableException
	 */
	private void playMarioSound() {

		Clip clip;

		try {

			clip = AudioSystem.getClip();

			AudioInputStream ais = AudioSystem
					.getAudioInputStream(getMarioSoundFile());

			clip.open((ais));			

			clip.start();

		} catch (LineUnavailableException e) {

			e.printStackTrace();
			
		} catch (FileNotFoundException e) {

			e.printStackTrace();
			
		} catch (IOException e) {

			e.printStackTrace();
			
		} catch (URISyntaxException e) {

			e.printStackTrace();
			
		} catch (UnsupportedAudioFileException e) {

			e.printStackTrace();
		}

	}
	
	
	/**
	 * Gets the Mario Sound File
	 * 
	 * @return
	 * 		the Mario Sound InputStream
	 * 
	 * Any of these exceptions are thrown if it can't get the sound file
	 * 
	 * @throws URISyntaxException 
	 * @throws URISyntaxException 
	 * @throws IOException 
	 * @throws UnsupportedAudioFileException 
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	private InputStream getMarioSoundFile() throws URISyntaxException, UnsupportedAudioFileException, IOException {
		
		//$$ to not cache it, but things start breaking when reusing a cached one
		return ClassLoader.getSystemResourceAsStream(MARIO_SOUND_CLASSPATH);

	}

	/**
	 * tester
	 * @param args
	 */
	public static void main(String[] args) {
		
		//doesn't work from here b/c main exits and kills the 
		// thread that was spawned to play the sound.  But the sound 
		// does work from the app, b/c it stays alive 
		INSTANCE.playMarioSoundInNewThread();	
		
	}

	
	



}
